package com.twu.biblioteca;

import java.util.Scanner;

/**
 * Created by Raluca Puichilita on 01/08/2014.
 */

public class MainMenu {

    private static boolean userIsLoggedIn = false;
    private static UserAccount loggedUser = null;

    protected static int retrieveUserMenuChoice (String messageDisplayedToUser) {
        System.out.println(messageDisplayedToUser);
        String userChoiceString = null;
        Scanner input = new Scanner(System.in);
        userChoiceString = input.next();
        int userChoiceInt = Integer.parseInt(userChoiceString);

        return userChoiceInt;
    }

    protected static String retrieveUserString (String messageDisplayedToUser) {
        System.out.println(messageDisplayedToUser);
        String userChoiceString = null;
        Scanner input = new Scanner(System.in);
        userChoiceString = input.next();

        return userChoiceString;
    }

    protected static void showRegisteredUserMenu() {
        System.out.println("------------------------------------------------");
        System.out.print("Welcome, user! Please select one of the following:\n");
        System.out.println("------------------------------------------------");

        System.out.print("(1) Check out book\n");
        System.out.print("(2) Check out movie\n");
        System.out.print("(3) Return book\n");
        System.out.print("(4) Return movie\n");
        System.out.print("(5) List items on loan\n");
        System.out.print("(6) View customer information\n");
        System.out.print("(7) Quit\n");
    }

    protected static void showMainMenu() {
        System.out.println("--------------");
        System.out.print("Main menu:\n");
        System.out.println("--------------");

        System.out.print("(1) List all books\n");
        System.out.print("(2) List all movies\n");
        System.out.print("(3) User login\n");
        System.out.print("(4) Quit\n");
    }

    protected static void runMainMenu() {
        if (userIsLoggedIn)
            showRegisteredUserMenu();
        else
            showMainMenu();
    }

    public void runMenuSelection(int userChoice, BookManager bibliotecaBookManager, MovieManager bibliotecaMovieManager, UserAccountManager bibliotecaUAManager) {
        if (userIsLoggedIn)
            userMenuSelection(userChoice, bibliotecaBookManager, bibliotecaMovieManager);
        else
            mainMenuSelection(userChoice, bibliotecaBookManager, bibliotecaMovieManager, bibliotecaUAManager);
    }

    protected static void userMenuSelection (int choice, BookManager myBookManager, MovieManager myMovieManager) {
        switch (choice) {
            case 1:
                userChecksOutBook(myBookManager);
                break;

            case 2:
                userChecksOutMovie(myMovieManager);
                break;

            case 3:
                userReturnsBook(myBookManager);
                break;

            case 4:
                userReturnsMovie(myMovieManager);
                break;

            case 5:
                loggedUser.listAllItemsOnLoanFromUser();
                break;

            case 6:
                showUserInformation();
                break;

            case 7:
                exitApp();
                break;

            default:
                invalidMenuSelection();
                break;
        }
    }

    private static void showUserInformation() {
        System.out.print(loggedUser);
    }

    protected static void mainMenuSelection(int choice, BookManager myBookManager, MovieManager myMovieManager, UserAccountManager uam) {
        switch(choice) {
            case 1:
                listAllBooks(myBookManager);
                break;

            case 2:
                listAllMovies(myMovieManager);
                break;

            case 3:
                userLogin(uam);
                break;

            case 4:
                exitApp();
                break;

            default:
                invalidMenuSelection();
                break;
        }
    }

    private static void userLogin(UserAccountManager uam) {
        String accno = retrieveUserString("Please input your account number in the form xxx-xxxx:\t");
        String pwd = retrieveUserString("And your password:\t");
        if (loginIsSuccessful(accno, pwd, uam)) {
            userIsLoggedIn = true;
            System.out.print("\nLogin successful!\n");
        }
        else
            System.out.print("\nLogin unsuccessful. User credentials could not be verified.\n");
    }

    private static void invalidMenuSelection() {
        System.out.println("\nSelect a valid option!\n");
    }

    private static void exitApp() {
        System.out.println("Thank you for using Biblioteca. Goodbye!");
        System.exit(0);
    }

    private static void listAllBooks(BookManager myBookManager) {
        if (myBookManager.collectionOfAvailableBooks.size() == 0)
            System.out.print("\nAll books are on loan at the minute. Please try again later.\n");
        else
            myBookManager.listAllBooksFromCollection(myBookManager.collectionOfAvailableBooks);
    }

    private static void listAllMovies(MovieManager myMovieManager) {
        if (myMovieManager.collectionOfAvailableMovies.size() == 0)
            System.out.print("\nAll movies are on loan at the minute. Please try again later.\n");
        else
            myMovieManager.listAllMoviesFromCollection(myMovieManager.collectionOfAvailableMovies);
    }

    private static void userChecksOutBook(BookManager myBookManager) {
        myBookManager.listAllBooksFromCollection(myBookManager.collectionOfAvailableBooks);

        if (myBookManager.collectionOfAvailableBooks.size() < 1)
            return;

        int bookToCheckOut = retrieveUserMenuChoice("\nInput the ID of the book to be checked out: ");

        if (!(bookToCheckOut > 0 && bookToCheckOut <= myBookManager.collectionOfAvailableBooks.size()))
            System.out.print("\nThat book is not available!\n");
        else {
            Book thisBook = myBookManager.collectionOfAvailableBooks.get(bookToCheckOut - 1);
            loggedUser.booksOnLoan.add(thisBook);
            myBookManager.checkOutBook(bookToCheckOut);
        }
    }

    private static void userChecksOutMovie(MovieManager myMovieManager) {
        myMovieManager.listAllMoviesFromCollection(myMovieManager.collectionOfAvailableMovies);

        if (myMovieManager.collectionOfAvailableMovies.size() < 1)
            return;

        int movieToCheckOut = retrieveUserMenuChoice("\nInput the ID of the movie to be checked out: ");

        if (!(movieToCheckOut > 0 && movieToCheckOut <= myMovieManager.collectionOfAvailableMovies.size()))
            System.out.print("\nThat is not a valid selection!");
        else {
            Movie thisMovie = myMovieManager.collectionOfAvailableMovies.get(movieToCheckOut - 1);
            loggedUser.moviesOnLoan.add(thisMovie);
            myMovieManager.checkoutMovie(movieToCheckOut);
        }
    }

    private static void userReturnsBook(BookManager myBookManager) {
        if (myBookManager.collectionOfUnavailableBooks.size() < 1)
            System.out.print("\nThere are no books on loan at the minute!\n");
        else {
            myBookManager.listAllBooksFromCollection(myBookManager.collectionOfUnavailableBooks);

            int bookToReturn = retrieveUserMenuChoice("\nWhich book would you like to return? ");

            if (!(bookToReturn > 0 && bookToReturn <= myBookManager.collectionOfUnavailableBooks.size()))
                System.out.print("\nThat is not a valid book to return!\n");
            else {
                Book thisBook = myBookManager.collectionOfUnavailableBooks.get(bookToReturn - 1);
                loggedUser.booksOnLoan.remove(thisBook);
                myBookManager.returnBook(bookToReturn);
            }
        }
    }

    private static void userReturnsMovie(MovieManager myMovieManager) {
        if (myMovieManager.collectionOfUnavailableMovies.size() < 1)
            System.out.print("\nThere are no movies on loan at the minute!\n");
        else {
            myMovieManager.listAllMoviesFromCollection(myMovieManager.collectionOfUnavailableMovies);

            int movieToReturn = retrieveUserMenuChoice("\nWhich movie would you like to return? ");

            if (!(movieToReturn > 0 && movieToReturn <= myMovieManager.collectionOfUnavailableMovies.size()))
                System.out.print("\nThat is not a valid movie to return!\n");
            else {
                Movie thisMovie = myMovieManager.collectionOfUnavailableMovies.get(movieToReturn - 1);
                loggedUser.moviesOnLoan.remove(thisMovie);
                myMovieManager.returnMovie(movieToReturn);
            }
        }
    }

    private static boolean loginIsSuccessful(String accno, String pwd, UserAccountManager uam) {
        if (uam.userCredentialsAreValid(accno, pwd) != null) {
            loggedUser = uam.userCredentialsAreValid(accno, pwd);
            return true;
        }
        return false;
    }
}
